<?php

declare(strict_types=1);

namespace App\Controller;

use App\Amqp\Producer\DemoProducer;
use Hyperf\Amqp\Producer;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;


/**
 * Class RabbitController
 * @package App\Controller
 * @AutoController()
 */
class RabbitController extends AbstractController
{
    public function index(RequestInterface $request, ResponseInterface $response)
    {
        $text = $request->input('text', 'RabbitMQ');
        $message = new DemoProducer(['time' => date('Y-m-d H:i:s'), 'text' => $text]);
        $producer = $this->container->get(Producer::class);
        $result = $producer->produce($message);

        $resultText = $result ? 'success' : 'fail';
        return $response->raw($resultText);
    }
}
